cmake_minimum_required(VERSION 3.5)

# ##############################################################################
# CMake Project ##
# ##############################################################################

# include useful macros
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/util.cmake)

# read metadata from package.xml and setup project
xict_cc_metadata()

# set project name and version
project(
  ${extracted_name}
  VERSION ${extracted_version}
  LANGUAGES C CXX)

# =========#
# Settings #
# =========#

# introduce default building settings
xict_cc_settings()

set(EXTRA_DEPS "")
set(EXTRA_INCLUDES "")

# =============#
# Dependencies #
# =============#

# ----------------------#
# Third-party libraries #
# ----------------------#
find_package(Protobuf REQUIRED)

# ##############################################################################
# Build ##
# ##############################################################################

# ==============#
# Build targets #
# ==============#
# 定义脚本文件的路径
set(MY_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_proto.sh")

# 确保脚本具有执行权限
execute_process(COMMAND chmod +x ${MY_SCRIPT})

# 执行脚本
execute_process(
  COMMAND ${MY_SCRIPT}
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  RESULT_VARIABLE MY_RESULT
  OUTPUT_VARIABLE MY_OUTPUT)

# 检查结果
if(NOT MY_RESULT EQUAL "0")
  message(FATAL_ERROR "Running script failed: ${MY_OUTPUT}")
endif()

# 打印脚本输出
message(STATUS "Script output: ${MY_OUTPUT}")

# 包含处理generate_export_header的模块
include(GenerateExportHeader)

# ---------------#
# Library target #
# ---------------#
include_directories(${PROJECT_SOURCE_DIR}/include
  ${PROJECT_SOURCE_DIR}/include/xict_proto)

# Find all source files
file(GLOB_RECURSE src_files ${PROJECT_SOURCE_DIR}/src/*/*.cc)

xict_cc_library(
  NAME
  ${PROJECT_NAME}
  SRCS
  ${src_files}
  INCLUDES
  ${EXTRA_INCLUDES}
  DEPS
  dl
  protobuf::libprotobuf
  ${EXTRA_DEPS})